<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>表单验证</title>
    <style>
        body{
            background-color: #F3EFE0;
        }
        .bot{
            color: #918E84;
            font-family:Arial, Helvetica, sans-serif;
            font-size: 24px;
            position: relative;
            right: 100px;
            float: right;
        }
        body {
            font-family: "仿宋";
        }
        table {
            position: relative;
            top: 50px;
            bottom: 100px;
            left: 500px;
        }
        table div {
            width: 380px;
            height: 60px;
            line-height: 60px;
            font-family: Arial;
        }

        table input {
            width: 320px;
            line-height: 25px;
            font-size: 16px;
        }

        th {
            font-size: 18px;
        }
        #btn {
            color:black;
            width: 100px;
            height: 50px;
        }
        .sum{
            width: 250px;
            margin: 100px auto;
        }
    </style>
</head>

<body>
    <h1 style="text-align: center; font-family: Arial;">填写注册信息</h1>
    <table>

        <tr>
            <th>用户名称:</th>
            <td><input type="text" name="username" placeholder="长度 4~12, 英文大小写字母">
            </td><td><div></div></td>
        </tr>
        <tr>
            <th>密&nbsp;&nbsp;&nbsp;&nbsp;码: </th>
            <td><input type="password" name="pwd" placeholder="长度 6~20大小写字母、数字或者下划线">
            </td><td><div></div></td>
        </tr>
        <tr>
            <th>确认密码: </th>
            <td><input type="password" name="repwd" placeholder="请再次输入密码进行确认">
            </td><td><div></div></td>
        </tr>
        <tr>
            <th>手机号码: </th>
            <td><input type="text" name="tel" placeholder="13、14、15、17、18开头的11位手机号">
            </td><td><div></div></td>
        </tr>
        <tr>
            <th>电子邮箱: </th>
            <td><input type="text" name="email" placeholder="用户名@域名(域名后缀至少2个字符)">
            </td><td><div></div></td>
        </tr>
    </table>
    <div class="sum">
        <div style="float: left; position: relative; right: 50px;"><input type="submit" value="注册" id="btn">
        </div>
        <div><input type="button" value="返回" id="btn" onclick=" location.href='../章节目录/第九章.html'">
        </div>
    </div>
    <script>
        var inputs = document.getElementsByTagName('input');
        for (var i = 0; i < inputs.length - 1; ++i) {
            inputs[i].onblur = inputBlur;
        }
        function inputBlur() {
            var name = this.name;
            var val = this.value;
            var tips = this.placeholder;
            var tips_obj = this.parentNode.nextSibling.firstChild;
            val = val.trim();
            if (!val) {
                error(tips_obj, '输入框不能为空');
                return false;
            }
            var reg_msg = getRegMsg(name, tips);
            if (reg_msg['reg'].test(val)) {
                success(tips_obj, reg_msg['msg']['success']);
            } else {
                error(tips_obj, reg_msg['msg']['error']);
            }
        }
        //显示验证失败提示信息
        function error(obj, msg) {
            obj.className = 'error';
            obj.innerHTML = msg + ',请重新输入';
        }
        //显示验证成功提示信息
        function success(obj, msg) {
            obj.className = 'success';
            obj.innerHTML = msg;
        }
        //获取每一个input组件对应的正则表达式以及匹配正确或错误的提示信息
        //返回一个对象, reg为正则表达式, msg为正确或者错误的对应信息
        function getRegMsg(name, tips) {
            var reg = msg = '';
            switch (name) {
                case 'username':
                    reg = /^[a-zA-Z]{4,12}$/;
                    msg = { 'success': '用户名输入正确', 'error': tips };
                    break;
                case 'pwd':
                    reg = /^\w{6,20}$/;
                    msg = { 'success': '密码输入正确', 'error': tips };
                    break;
                case 'repwd':
                    var con = document.getElementsByTagName('input')[1].value;
                    reg = RegExp('^' + con + '$');
                    msg = { 'success': '两次密码输入正确', 'error': '两次输入的密码不一致' };
                    break;
                case 'tel':
                    reg = /^1[34578]\d{9}$/;
                    msg = { 'success': '手机号码输入正确', 'error': tips };
                    break;
                case 'email':
                    reg = /^(\w+(\_|\-|\.)*)+@(\w+(\-)?)+(\.\w{2, })+$/;
                    msg = { 'success': '邮箱输入正确', 'error': tips };
                    break;
            }
            return { 'reg': reg, 'msg': msg };
        }
    </script>
    <div class="bot">
        <h3>Please appreciate((●'◡'●))......</h3>
    </div>
</body>

</html>